import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {BannerProvider} from "../../../providers/index/banner/banner.provider";
import {BannerDetail} from "../../../model/pages/index/banner-detail/banner-detail.model";
import {DomSanitizer} from "@angular/platform-browser";
import {LoadingService} from "../../../services/common/loading.service";
import {FileService} from "../../../services/native/file/file.service";
import {CacheImgService} from "../../../services/native/cache-img/cache-img.service";

@IonicPage()
@Component({
    selector: 'page-banner',
    templateUrl: 'banner.html',
    providers: [
        BannerProvider
    ]
})
export class BannerPage {

    // 当前信息id
    private contentId: any;
    // banner详情
    private bannerDetail: BannerDetail = new BannerDetail();

    constructor(private navParams: NavParams,
                private navCtrl: NavController,
                private sanitizer: DomSanitizer,
                private bannerProvider: BannerProvider,
                private toastCtrl: ToastController,
                private fileService: FileService,
                private load: LoadingService,
                private cacheImg: CacheImgService
                ) {

    }

    ionViewDidLoad() {
        this.contentId = this.navParams.get('id');
        this.getInfoDetail(this.contentId);


    }

    // 获取详情
    getInfoDetail(id) {
        this.load.showLoading();
        this.bannerProvider.getBannerDetail(id).subscribe((res) => {
            let sourceUrl = res.linkUrl,
                linkUrl = sourceUrl ? this.sanitizer.bypassSecurityTrustResourceUrl(sourceUrl) : sourceUrl;

            let imgUrl=res.imgUrl;
            if(imgUrl) {
                // this.fileService.getFilePath(res.imgUrl).subscribe((res) => {
                //
                // });
                // ele.realImgUrl = this.fileService.getCompletePath(imgUrl, data.azureInfo);
                this.fileService.getAzureInfo().subscribe((data) => {
                    this.cacheImg.getImgPath(imgUrl, data.azureInfo).then((res) => {
                        this.bannerDetail['imageUrl'] = res;
                    }).catch(() =>{
                        this.bannerDetail['imageUrl']= this.fileService.getCompletePath(imgUrl, data.azureInfo);
                    });
                    this.bannerDetail['imageUrl']=res.sasUrl;
                });
            }
            this.bannerDetail = {
                "id": res.id,
                "title": res.title,
                "content": res.content,
                "imageUrl": './assets/images/index/banner_default.jpg',
                "linkUrl": linkUrl
            }
            this.load.dismissLoading();
        }, error => {
            this.load.dismissLoading();
        })
    }

    /**
     * 提示框
     *
     * @private
     * @param {any} msg 提示的消息
     */
    showToast(msg) {
        let toast = this.toastCtrl.create({
            message: msg,
            position: 'middle',
            duration: 1200
        });
        toast.present();
        toast.dismissAll();
    }
}
